package com.pentofab.payrollmanagement.ui.controller;

import java.util.Date;
import java.util.HashMap;

import com.pentofab.payrollmanagement.application.PayRollApplication;
import com.pentofab.payrollmanagement.application.PayrollApplicationFactory;
import com.pentofab.payrollmanagement.exception.BusinessException;
import com.pentofab.payrollmanagement.utilities.PayRollUtility;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * The Main UI controller. Contains the common elements across the application
 * (Header, Footer). Also maintains the UI flow between different screens.
 * 
 * @author AjayP
 * 
 */
public class MainController extends AbstractUIController {

	public static final String ADD_Units = "AddUnits.fxml";
	public static final String ADD_DEPARTMENT_SCREEN = "AddDepartment.fxml";
	public static final String ADD_DESIGNATION_SCREEN = "AddDesignation.fxml";
	public static final String ADD_EMPLOYEE_CATEGORY_SCREEN = "AddEmpCategory.fxml";
	public static final String ADD_COMPANY_SCREEN = "AddCompany.fxml";
	public static final String ADD_EMPLOYEE_SCREEN = "AddEmployee.fxml";
	public static final String COMPANIES_LIST_SCREEN = "CompaniesList.fxml";
	public static final String COMPANY_DASH_BOARD_SCREEN = "CompanyDashBoard.fxml";
	public static final String ATTANDANCE_SCREEN = "Attendance.fxml";
	public static final String LOGIN_FORM = "Login.fxml";
	public static final String Leave_FORM = "Leave.fxml";
	public static final String DISPLAY_ATTENDANCE = "Dislpay_Attendance.fxml";
	public static final String USERACCOUNTS = "UserAccountsHandler.fxml";
	public static final String ManageUserAccounts = "ManageUserAccounts.fxml";
	public static final String SalaryDisplayAttendance = "SalaryDisplayAttendance.fxml";
	public static final String viewSalaryProfile = "ViewSalaryProfiles.fxml"; 
	public static final String SalaryAdvance = "SalaryAdvance.fxml";
	public static final String SalaryProfile = "SalaryProfile.fxml";
	public static final String SalaryStracture = "SalaryStracture.fxml";
	public static final String DisplaySalaryStracture = "SalaryDisplaySalaryStracture.fxml";
	public static final String SalaryReport = "SalaryReport.fxml";
	public static final String BankInfo = "BankInfo.fxml";
	public static final String AddBankInfo = "AddBankInfo.fxml";
//	public static final String SalaryDisplayAttendance = "SalaryDisplayAttendance.fxml";

	@FXML
	private Parent root;
	@FXML
	private BorderPane contentArea;
	@FXML
	private Label titleField;
	@FXML
	private Label dateLabel;
	@FXML
	private Label userNameLabel;
	@FXML
	private Button LogOut;

	private Stage stage;

	private StackPane stackPane;

	private String currentScreen = "";

	private HashMap<String, AbstractUIController> screens = 
			new HashMap<String, AbstractUIController>();

	public MainController() {
		stackPane = new StackPane();
	}

	public void logOut(){
		
		/*if(!showConfirmDialog(UIMessages.CONFORM_LOGOUT)){
			return;
		}*/
		PayRollUtility.debug(userNameLabel.getText()+" loged out on "+new Date());
		screens.clear();
		stackPane.getChildren().clear();
		MainController mainController = PayrollApplicationFactory.getMainController();
		mainController.showScreen(MainController.LOGIN_FORM, false,mainController);
		mainController.refreshUI("");
		
		LoginController.isForgedLogin = false;
	}
	
	public void exit(){
		
		if(showConfirmDialog(UIMessages.CONFORM_EXIT)){
			if(userNameLabel.getText() != null){
				PayRollUtility.debug(userNameLabel.getText()+" exited on "+new Date());
			}
			System.exit(0);
		}
		
		
	}
	
	public boolean loadScreen(String fxml) {
		System.out.println("fxml : "+fxml);
		if (!screens.containsKey(fxml)) {
			try {
				FXMLLoader loader = new FXMLLoader();
				loader.load(loader.getClass().getResourceAsStream(
						"/fxml/" + fxml));
				AbstractUIController uiController = loader.getController();
				addScreen(fxml, uiController);
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return false;
	}

	public boolean unloadScreen(String screenName) {
		if (screens.remove(screenName) == null) {
			return false;
		} else {
			return true;
		}
	}

	public void addScreen(String name, AbstractUIController screen) {
		screens.put(name, screen);
	}

	public void setStage(Stage stage) {
		this.stage = stage;
	}

	public void showScreen(String screenName, boolean withHistory,
			Object... args) {
		if (!screens.containsKey(screenName)) {
			loadScreen(screenName);
		}

		if (stackPane.getChildren().size() > 0) {
			stackPane.getChildren().remove(0);
		}
		AbstractUIController controller = screens.get(screenName);
		if (withHistory && currentScreen.length() > 0) {
			controller.setPartent(currentScreen);
		}
		controller.refreshUI(args);
		if(args != null && args.length > 0){
			System.out.println("MAIn ; "+args[0]);
		}
		stackPane.getChildren().add(controller.getView());
		contentArea.setCenter(stackPane);
		titleField.setText(controller.getTitle());
		this.stage.setTitle(controller.getTitle());
		currentScreen = screenName;
		try {
			dateLabel.setText(PayRollUtility.convertDateToString(new Date()));
		} catch (BusinessException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Parent getView() {
		return root;
	}

	@Override
	public String getTitle() {
		return "";
	}

	@Override
	public void refreshUI(Object... args) {
	
		if(args != null && args.length > 0){
			userNameLabel.setText(args[0].toString());
		}
	}
	
}
